package com.lhz.collector.controller;

import com.alibaba.fastjson.JSON;
import com.lhz.collector.entity.ParseRule;
import com.lhz.collector.service.ParseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LHZ on 2017/3/24.
 */
@RestController
@RequestMapping("/api")
public class ParseController {
    @Autowired
    private ParseService parseService;
    //查看具体规则
    @RequestMapping("/rules")
    public ResponseEntity getRules(){
        List<ParseRule> list=parseService.findAllList();
        List<Map<String,Object>> resuleList=new ArrayList<>();
        for(ParseRule parseRule:list){
            Map<String,Object> map=new HashMap();
            map.put("id",parseRule.getId());
            String ruleBody=parseRule.getRulebody();
            Map ruleMap= (Map) JSON.parse(ruleBody);
            map.put("name",ruleMap.get("name"));
            resuleList.add(map);
        }
        return ResponseEntity.ok(resuleList);
    }

    //获取所有解析规则
    @RequestMapping("/logrules")
    public ResponseEntity getLogrules(){
        List<ParseRule> list=parseService.findAllList();
        return ResponseEntity.ok(list);
    }
    //获取单条解析规则
    @RequestMapping("/logrule/{id}")
    public ResponseEntity getLogruleById(@PathVariable Long id){
        ParseRule parseRule=parseService.getById(id);
        return ResponseEntity.ok(parseRule);
    }
    //保存解析规则
    @RequestMapping(value = "/logrule",method = RequestMethod.POST)
    public ResponseEntity save(@RequestBody ParseRule parseRule){
        if(parseService.insert(parseRule)){
            return ResponseEntity.ok("保存成功");
        }else{
            return ResponseEntity.badRequest().body("保存失败");
        }
    }
    //修改解析规则
    @RequestMapping(value = "/logrule/{id}",method = RequestMethod.PUT)
    public ResponseEntity update(@PathVariable Long id,@RequestBody ParseRule parseRule){
        if(parseService.update(parseRule)){
            return ResponseEntity.ok("修改成功");
        }else{
            return ResponseEntity.badRequest().body("修改失败");
        }
    }
    //删除解析规则
    @RequestMapping(value = "/logrule/{id}",method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable int id){
        ParseRule parseRule=new ParseRule();
        parseRule.setId(id);
        if(parseService.delete(parseRule)){
            return ResponseEntity.ok("删除成功");
        }else{
            return ResponseEntity.badRequest().body("删除失败");
        }
    }
    //预览
    @RequestMapping("/logrule/preview")
    public ResponseEntity preview(@RequestBody String jsonRule){
        List<AnalyResult> list = new ArrayList<>();
        Map<String,Object> map=parseService.preview(jsonRule);
        for(Map.Entry<String,Object> entry: map.entrySet()){
            AnalyResult ar = new AnalyResult();
            ar.setFieldName(entry.getKey());
            ar.setValue(entry.getValue());
            list.add(ar);
        }
        return ResponseEntity.ok(list);
    }

    private class AnalyResult {
        private String fieldName;
        private String CNName;
        private String type = "text";
        private String format;
        private Object value;

        public String getFieldName() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }

        public String getCNName() {
            return CNName;
        }

        public void setCNName(String CNName) {
            this.CNName = CNName;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getFormat() {
            return format;
        }

        public void setFormat(String format) {
            this.format = format;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }
    }

}
